#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

MACRO(TRY_COMPILE_MOZJS _test_program _definitions _link_flags)
  TRY_COMPILE(TRY_COMPILE_MOZJS_RESULT
    ${CMAKE_BINARY_DIR}/configure
    ${CMAKE_SOURCE_DIR}/cmake/${_test_program}
    CMAKE_FLAGS
      -DINCLUDE_DIRECTORIES:STRING=${MOZJS_INCLUDE_DIR}
      -DLINK_DIRECTORIES:STRING=${MOZJS_LINK_DIR}
      -DCMAKE_EXE_LINKER_FLAGS:STRING=${_link_flags}
      -DLINK_LIBRARIES:STRING=${MOZJS_LIBRARIES}
    COMPILE_DEFINITIONS ${_definitions}
    OUTPUT_VARIABLE TRY_COMPILE_MOZJS_OUTPUT)
  # MESSAGE("'${TRY_COMPILE_MOZJS_RESULT}' '${TRY_COMPILE_MOZJS_RESULT}'")
ENDMACRO(TRY_COMPILE_MOZJS _test_program _definitions _link_flags)

MACRO(TRY_RUN_MOZJS _test_program _definitions _link_flags)
  TRY_RUN(TRY_RUN_MOZJS_RESULT TRY_RUN_MOZJS_COMPILE_RESULT
    ${CMAKE_BINARY_DIR}/configure
    ${CMAKE_SOURCE_DIR}/cmake/${_test_program}
    CMAKE_FLAGS
      -DINCLUDE_DIRECTORIES:STRING=${MOZJS_INCLUDE_DIR}
      -DLINK_DIRECTORIES:STRING=${MOZJS_LINK_DIR}
      -DCMAKE_EXE_LINKER_FLAGS:STRING=${_link_flags}
      -DLINK_LIBRARIES:STRING=${MOZJS_LIBRARIES}
    COMPILE_DEFINITIONS ${_definitions}
    OUTPUT_VARIABLE TRY_RUN_MOZJS_OUTPUT)
  # MESSAGE("'${TRY_RUN_MOZJS_COMPILE_RESULT}' '${TRY_RUN_MOZJS_RESULT}'" '${TRY_RUN_MOZJS_OUTPUT}')
ENDMACRO(TRY_RUN_MOZJS _test_program _definitions _link_flags)

IF(MOZJS_FOUND)
  IF(UNIX)
    SET(MOZJS_DEFINITIONS "${MOZJS_DEFINITIONS} -DXP_UNIX")
  ENDIF(UNIX)

  TRY_COMPILE_MOZJS(
    test_js_threadsafe.c
    "${MOZJS_DEFINITIONS} -DMIN_MOZJS_VERSION=160 -DJS_THREADSAFE"
    "${CMAKE_EXE_LINKER_FLAGS} ${MOZJS_LINKER_FLAGS}")

  IF(TRY_COMPILE_MOZJS_RESULT)
    SET(MOZJS_DEFINITIONS "${MOZJS_DEFINITIONS} -DJS_THREADSAFE")
  ELSE(TRY_COMIPLE_MOZJS_RESULT)
    TRY_COMPILE_MOZJS(
      test_js_threadsafe.c
      "${MOZJS_DEFINITIONS} -DMIN_MOZJS_VERSION=160"
      "${CMAKE_EXE_LINKER_FLAGS} ${MOZJS_LINKER_FLAGS}")
    IF(NOT TRY_COMPILE_MOZJS_RESULT)
      SET(MOZJS_FOUND 0)
      MESSAGE("Failed to try run SpiderMonkey, the library version may be too low.")
    ENDIF(NOT TRY_COMPILE_MOZJS_RESULT)
  ENDIF(TRY_COMPILE_MOZJS_RESULT)
ENDIF(MOZJS_FOUND)

SET(MOZJS_LIST
  mozilla-js
  xulrunner-js
  firefox2-js
  firefox-js
)
FOREACH(MOZJS_LIB ${MOZJS_LIST})
  IF(NOT MOZJS_FOUND)
    GET_CONFIG(${MOZJS_LIB} 0 MOZJS MOZJS_FOUND)
    IF(MOZJS_FOUND)
      SET(MOZJS_NAME ${MOZJS_LIB})
    ENDIF(MOZJS_FOUND)
  ENDIF(NOT MOZJS_FOUND)
ENDFOREACH(MOZJS_LIB ${MOZJS_LIST})

IF(MOZJS_FOUND)
  TRY_RUN_MOZJS(
    test_js_mozilla_1_8_branch.c
    "${MOZJS_DEFINITIONS} -DMOZILLA_1_8_BRANCH"
    "${CMAKE_EXE_LINKER_FLAGS} ${MOZJS_LINKER_FLAGS}")

  IF(TRY_RUN_MOZJS_COMPILE_RESULT AND "${TRY_RUN_MOZJS_RESULT}" STREQUAL "0")
    SET(MOZJS_DEFINITIONS "${MOZJS_DEFINITIONS} -DMOZILLA_1_8_BRANCH")
  ENDIF(TRY_RUN_MOZJS_COMPILE_RESULT AND "${TRY_RUN_MOZJS_RESULT}" STREQUAL "0")
ENDIF(MOZJS_FOUND)

# Hell, some distributions' package of mozjs is broken. We have to fix it here
IF(MOZJS_FOUND)
  STRING(REGEX MATCH "/stable" RESULT "${MOZJS_INCLUDE_DIR}")
  IF("${RESULT}" STREQUAL "/stable")
    PKG_GET_VARIABLE(${MOZJS_NAME} "includedir" MOZJS_INC)
    FIND_FILE(JSAPI_PATH jsapi.h "${MOZJS_INC}/unstable")
    IF(JSAPI_PATH)
      LIST(APPEND MOZJS_INCLUDE_DIR "${MOZJS_INC}/unstable")
    ENDIF(JSAPI_PATH)
  ENDIF("${RESULT}" STREQUAL "/stable")
ENDIF(MOZJS_FOUND)
